package com.ruoyi.project.vehicle.service.impl;

import java.util.List;
import java.util.Map;
import java.util.Objects;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.project.user.domain.UserInfo;
import com.ruoyi.project.user.service.IUserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.stereotype.Service;
import com.ruoyi.project.vehicle.mapper.BinsVehicleInfoMapper;
import com.ruoyi.project.vehicle.domain.BinsVehicleInfo;
import com.ruoyi.project.vehicle.service.IBinsVehicleInfoService;

/**
 * 车辆信息Service业务层处理
 * 
 * @author Administrator
 * @date 2025-04-01
 */
@Service
public class BinsVehicleInfoServiceImpl implements IBinsVehicleInfoService 
{
    @Autowired
    private BinsVehicleInfoMapper binsVehicleInfoMapper;
    @Autowired
    private IUserInfoService userInfoService;
    /**
     * 查询车辆信息
     * 
     * @param vehicleCode 车辆信息主键
     * @return 车辆信息
     */
    @Override
    public BinsVehicleInfo selectBinsVehicleInfoByVehicleCode(Long vehicleCode)
    {
        return binsVehicleInfoMapper.selectBinsVehicleInfoByVehicleCode(vehicleCode);
    }

    /**
     * 查询车辆信息列表
     * 
     * @param binsVehicleInfo 车辆信息
     * @return 车辆信息
     */
    @Override
    public List<BinsVehicleInfo> selectBinsVehicleInfoList(BinsVehicleInfo binsVehicleInfo)
    {
        return binsVehicleInfoMapper.selectBinsVehicleInfoList(binsVehicleInfo);
    }

    /**
     * 新增车辆信息
     * 
     * @param binsVehicleInfo 车辆信息
     * @return 结果
     */
    @Override
    public int insertBinsVehicleInfo(BinsVehicleInfo binsVehicleInfo)
    {
        binsVehicleInfo.setCreateTime(DateUtils.getNowDate());
        return binsVehicleInfoMapper.insertBinsVehicleInfo(binsVehicleInfo);
    }

    /**
     * 修改车辆信息
     * 
     * @param binsVehicleInfo 车辆信息
     * @return 结果
     */
    @Override
    public int updateBinsVehicleInfo(BinsVehicleInfo binsVehicleInfo)
    {
        binsVehicleInfo.setUpdateTime(DateUtils.getNowDate());
        return binsVehicleInfoMapper.updateBinsVehicleInfo(binsVehicleInfo);
    }

    /**
     * 批量删除车辆信息
     * 
     * @param vehicleCodes 需要删除的车辆信息主键
     * @return 结果
     */
    @Override
    public int deleteBinsVehicleInfoByVehicleCodes(Long[] vehicleCodes)
    {
        return binsVehicleInfoMapper.deleteBinsVehicleInfoByVehicleCodes(vehicleCodes);
    }

    /**
     * 删除车辆信息信息
     * 
     * @param vehicleCode 车辆信息主键
     * @return 结果
     */
    @Override
    public int deleteBinsVehicleInfoByVehicleCode(Long vehicleCode)
    {
        return binsVehicleInfoMapper.deleteBinsVehicleInfoByVehicleCode(vehicleCode);
    }

    @Override
    public BinsVehicleInfo getByQrCode(String qrCode) {
        return binsVehicleInfoMapper.getByQrCode(qrCode);
    }

    @Override
    public BinsVehicleInfo getByLicenseNo(String licenseNo) {
        return binsVehicleInfoMapper.getByLicenseNo(licenseNo);
    }

    @Override
    public List<Map> getVehicleHeadMap(Long deptId) {
        return binsVehicleInfoMapper.getVehicleHeadMap(deptId);
    }

    @Override
    public boolean userUnlock(Map dto) {
        String vehicleCode = null;
        String userId = null;
        try {

            if ( Objects.isNull(dto.get("vehicleCode"))){
                throw new ServiceException("车辆编码不可为空");
            }
            vehicleCode = dto.get("vehicleCode").toString();

            if (Objects.isNull(dto.get("userId"))){
                throw new ServiceException("用户编码不可为空");
            }
            userId = dto.get("userId").toString();
        } catch (RuntimeException e) {
            throw new ServiceException("输入参数有误");
        }
        UserInfo userInfo = userInfoService.selectUserInfoByUserId(userId);
        if (userInfo==null){
            throw new RuntimeException("用户不存在");
        }
        if ("1".equals(userInfo.getPhotoStatus())){
            throw new RuntimeException("用户审核未通过，请更新信息后再使用");
        }
        BinsVehicleInfo binsVehicleInfo = binsVehicleInfoMapper.selectBinsVehicleInfoByVehicleCode(Long.valueOf(vehicleCode));
        if (binsVehicleInfo==null){
            throw new RuntimeException("车辆不存在");
        }
        if (binsVehicleInfo.getStatus()!=0){
            throw new RuntimeException("车辆维修中，请选择其它车辆");
        }
        if (0==(binsVehicleInfo.getLockFlag())){
            if (userId.equals(binsVehicleInfo.getWorkUser())){
                return true;
            }
            throw new RuntimeException("车辆已锁定");
        }
        BinsVehicleInfo binsVehicleInfo_update = new BinsVehicleInfo();
        binsVehicleInfo_update.setVehicleCode(Long.valueOf(vehicleCode));
        binsVehicleInfo_update.setLockFlag(0);
        binsVehicleInfo_update.setWorkUser(userId);
        binsVehicleInfo_update.setWorkStatus("1");
        int count = binsVehicleInfoMapper.updateBinsVehicleInfo(binsVehicleInfo_update);
        if (count==1) {
            return true;
        }
        return false;
    }


}
